'''
Created on 22/05/2012

@author: Bruna, Bruno
'''

BAUDRATE = 9600

import serial
import sys

def measure(port):
    port.write('M')
    observed_color, value = tuple(port.readline().split(' '))
    return (observed_color, float(value))

if __name__ == "__main__":
    num_measurements_accuracy = 1000
    
    try:
        index = sys.argv.index('-m')
        if (index > -1):
            num_measurements_accuracy = int(sys.argv[index + 1])
    except ValueError:
        pass
        
    port_name = "COM3"
    try:
        index = sys.argv.index('-p')
        if (index > -1):
            port_name = sys.argv[index + 1]
    except ValueError:
        pass

    port = serial.Serial(port_name, BAUDRATE)
    print "Connection opened."
   
    # Handshake
    port.write('A')
    print "Handshake completed"
    
    while True:
        cmd = raw_input()
        
        if (len(cmd) == 1):
            if (cmd == 'X'):
                break
            elif (cmd == 'D' or cmd == 'L'):
                port.write(cmd)
                continue
        else:
            color = cmd[0]
            right_guess = 0
            min_guess = 50000.0;
            for i in xrange(num_measurements_accuracy):
                observed_color, value = measure(port)
                if color == observed_color:
                    right_guess += 1
                    min_guess = min(min_guess, value)
                
            print color, right_guess / float(num_measurements_accuracy), min_guess
    port.close()